package controller

import (
	"goscan-mgt/model"
	"goscan-mgt/service"
	"goscan-mgt/utils"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

type ScanController struct {
	scanService *service.ScanService
}

func NewScanController() *ScanController {
	scanService := service.NewScanService()
	return &ScanController{scanService: scanService}
}

// CreateScanResult 创建扫描结果
// @Summary 创建扫描结果
// @Description 创建一个新的扫描结果
// @Tags 扫描结果
// @Accept json
// @Produce json
// @Param scanResult body model.ScanResult true "扫描结果对象"
// @Success 201 {object} utils.Response
// @Failure 400 {object} utils.Response
// @Router /scan_result [post]
func (sc *ScanController) CreateScanResult(c *gin.Context) {
	var scanResult model.ScanResult
	if err := c.ShouldBindJSON(&scanResult); err != nil {
		c.JSON(http.StatusBadRequest, utils.NewBuilder().Error(err.Error()).Build())
		return
	}
	if err := sc.scanService.CreateScanResult(&scanResult); err != nil {
		c.JSON(http.StatusInternalServerError, utils.NewBuilder().Error(err.Error()).Build())
		return
	}
	c.JSON(http.StatusCreated, utils.NewBuilder().Success(scanResult).Build())
}

// GetScanResultByID 获取扫描结果
// @Summary 获取扫描结果
// @Description 根据ID获取扫描结果
// @Tags 扫描结果
// @Produce json
// @Param id path int true "扫描结果ID"
// @Success 200 {object} utils.Response
// @Failure 400 {object} utils.Response
// @Failure 404 {object} utils.Response
// @Router /scan_result/{id} [get]
func (sc *ScanController) GetScanResultByID(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 64)
	if err != nil {
		c.JSON(http.StatusBadRequest, utils.NewBuilder().Error("无效的ID").Build())
		return
	}
	result, err := sc.scanService.GetScanResultByID(uint(id))
	if err != nil {
		c.JSON(http.StatusNotFound, utils.NewBuilder().Error("未找到该扫描结果").Build())
		return
	}
	c.JSON(http.StatusOK, utils.NewBuilder().Success(result).Build())
}

// UpdateScanResult 更新扫描结果
// @Summary 更新扫描结果
// @Description 更新指定ID的扫描结果
// @Tags 扫描结果
// @Accept json
// @Produce json
// @Param id path int true "扫描结果ID"
// @Param scanResult body model.ScanResult true "扫描结果对象"
// @Success 200 {object} utils.Response
// @Failure 400 {object} utils.Response
// @Failure 404 {object} utils.Response
// @Router /scan_result/{id} [put]
func (sc *ScanController) UpdateScanResult(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 64)
	if err != nil {
		c.JSON(http.StatusBadRequest, utils.NewBuilder().Error("无效的ID").Build())
		return
	}
	var scanResult model.ScanResult
	if err := c.ShouldBindJSON(&scanResult); err != nil {
		c.JSON(http.StatusBadRequest, utils.NewBuilder().Error(err.Error()).Build())
		return
	}
	scanResult.ID = uint(id)
	if err := sc.scanService.UpdateScanResult(&scanResult); err != nil {
		c.JSON(http.StatusNotFound, utils.NewBuilder().Error(err.Error()).Build())
		return
	}
	c.JSON(http.StatusOK, utils.NewBuilder().Success(scanResult).Build())
}

// DeleteScanResult 删除扫描结果
// @Summary 删除扫描结果
// @Description 根据ID删除扫描结果
// @Tags 扫描结果
// @Param id path int true "扫描结果ID"
// @Success 204 {object} utils.Response
// @Failure 400 {object} utils.Response
// @Failure 404 {object} utils.Response
// @Router /scan_result/{id} [delete]
func (sc *ScanController) DeleteScanResult(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 64)
	if err != nil {
		c.JSON(http.StatusBadRequest, utils.NewBuilder().Error("无效的ID").Build())
		return
	}
	if err := sc.scanService.DeleteScanResult(uint(id)); err != nil {
		c.JSON(http.StatusNotFound, utils.NewBuilder().Error(err.Error()).Build())
		return
	}
	c.JSON(http.StatusNoContent, utils.NewBuilder().Success(nil).Build())
}

// GetAllScanResults 获取所有扫描结果
// @Summary 获取所有扫描结果
// @Description 获取所有扫描结果列表，支持分页查询
// @Tags 扫描结果
// @Produce json
// @Param page query int false "页码" default(1)
// @Param page_size query int false "每页数量" default(10)
// @Success 200 {object} utils.Response "成功返回分页的扫描结果列表"
// @Failure 500 {object} utils.Response "服务器内部错误"
// @Router /scan_result [get]
func (sc *ScanController) GetAllScanResults(c *gin.Context) {
	// 解析分页参数
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "10"))

	results, err := sc.scanService.GetPaginatedScanResults(page, pageSize)
	if err != nil {
		c.JSON(http.StatusInternalServerError, utils.NewBuilder().Error(err.Error()).Data(results).Build())
		return
	}
	c.JSON(http.StatusOK, utils.NewBuilder().Success(results).Build())
}

// GetScanResultsByProjectID 根据项目ID获取扫描结果
// @Summary 根据项目ID获取扫描结果
// @Description 根据项目ID获取扫描结果列表，支持分页查询
// @Tags 扫描结果
// @Produce json
// @Param project_id path int true "项目ID"
// @Param page query int false "页码" default(1)
// @Param page_size query int false "每页数量" default(10)
// @Success 200 {object} utils.Response "成功返回分页的扫描结果列表"
// @Failure 500 {object} utils.Response "服务器内部错误"
// @Router /scan_result/project/{project_id} [get]
func (sc *ScanController) GetScanResultsByProjectID(c *gin.Context) {
	// 解析分页参数
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "10"))
	projectID := c.Param("project_id")
	results, err := sc.scanService.GetScanResultsByProjectID(page, pageSize, projectID)
	if err != nil {
		c.JSON(http.StatusInternalServerError, utils.NewBuilder().Error(err.Error()).Data(results).Build())
		return
	}
	c.JSON(http.StatusOK, utils.NewBuilder().Success(results).Build())
}
